// pages/mine/index.js

import { ClassicModel } from '../../models/classic.js'
import { BookModel } from '../../models/book.js'
import { promisic } from '../../utils/common.js'

const classicModel = new ClassicModel()
const bookModel = new BookModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    authorized: false,
    userInfo: null,
    bookCount: 0,
    classics: null
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.userAuthorized()
    this.getMyFavor()
    this.getMyBookCount()
  },

  userAuthorized: async function() {
    const data = await promisic(wx.getSetting)()
    if (data.authSetting['scope.userInfo']) {
      const res = await promisic(wx.getUserInfo)()
      const userInfo = res.userInfo
      this.setData({
        authorized: true,
        userInfo
      })
    }
  },


  getMyFavor: function() {
    classicModel.getMyFavor(res => this.setData({ classics: res }))
  },

  getMyBookCount: async function() {
    const data = await bookModel.getMyBookCount()
    this.setData({bookCount: data.count})
  },

  onJumpToDetail: function(evt) {
    const { cid, type } = evt.detail
    wx.navigateTo({
      url: `/pages/classic/detail/index?cid=${cid}&type=${type}`,
    })
  }
})